package br.ufrj.dcc.smarthotel.activities;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import br.ufrj.dcc.smarthotel.R;
import br.ufrj.dcc.smarthotel.controls.InvitePeopleControl;
import br.ufrj.dcc.smarthotel.entities.EventUser;
import br.ufrj.dcc.smarthotel.listeners.InvitePeopleItemClickListener;

public class InvitePeopleActivity extends AppActivity {
	private InvitePeopleControl control;
	private InvitePeopleItemClickListener listener;
	
	private List<EventUser> eventUserList;
	private int clickedEventId;
	
	private ListView listPeople;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        this.clickedEventId = Integer.parseInt(this.getIntent().getExtras().getString("clickedEventId"));
        
        setContentView(R.layout.invitepeople);
        
        this.findAllViews();
        this.control = new InvitePeopleControl(this);
        this.listener = new InvitePeopleItemClickListener(this);
        
        this.listPeople.setOnItemClickListener(this.listener);
        
        this.control.loadAvailablePeople(clickedEventId);
    }

	private void findAllViews() {
		this.listPeople = (ListView) this.findViewById(R.id.listPeople);
	}

	public void setEventUserList(List<EventUser> eventUserList) {
		this.eventUserList = eventUserList;
		
		this.listPeople.setAdapter(new EventUserListAdapter(this.getApplicationContext(), R.id.listPeople, this.eventUserList));
	}
	
	class EventUserListAdapter extends ArrayAdapter<EventUser> {
		List<EventUser> eventUserList;
		
		public EventUserListAdapter(Context context, int textViewResourceId, List<EventUser> objects) {
			super(context, textViewResourceId, objects);
			
			this.eventUserList = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            
	            v = vi.inflate(R.layout.people_item, null);
	        }
	        
	        EventUser eventUser = eventUserList.get(position);
	        
	        if (eventUser != null) {
                TextView txtPersonName = (TextView) v.findViewById(R.id.txtPersonName);
                TextView txtPersonEmail = (TextView) v.findViewById(R.id.txtPersonEmail);
                
                txtPersonName.setText(eventUser.getUser().getName());
                txtPersonEmail.setText(eventUser.getUser().getEmail());
	        }
	        
	        return v;
		}
	}

	public List<EventUser> getEventUserList() {
		return this.eventUserList;
	}
	
	public int getClickedEventId() {
		return this.clickedEventId;
	}

	public void inviteEventUser(EventUser clickedEventUser) {
		this.control.inviteEventUser(clickedEventUser);
	}
}
